from datetime import datetime

import execjs
import requests

now = datetime.now()
timestamp = int(now.timestamp() * 1000)
client = 'fanyideskweb'
product = 'webfanyi'
print(timestamp)

query = '把'

headers = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Origin': 'https://fanyi.youdao.com',
    'Pragma': 'no-cache',
    'Referer': 'https://fanyi.youdao.com/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0',
    'sec-ch-ua': '"Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'cookie': 'OUTFOX_SEARCH_USER_ID=-2034389685@10.110.96.157; OUTFOX_SEARCH_USER_ID_NCOO=1994625046.2245197',
}

sign = execjs.compile(open("./main.js", "r", encoding='utf-8').read()).call("GetSign", timestamp, client, product,
                                                                            'asdjnjfenknafdfsdfsd')

response = requests.get(
    'https://dict.youdao.com/webtranslate/key?keyid=webfanyi-key-getter&sign=' + sign + '&client=' + client + '&product=' + product + '&appVersion=1.0.0&vendor=web&pointParam=client,mysticTime,product&mysticTime=' + str(
        timestamp) + '&keyfrom=fanyi.web&mid=1&screen=1&model=1&network=wifi&abtest=0&yduuid=abcdefg',
    headers=headers
)

print(response.json())

aesKey = response.json()["data"]["aesKey"]
aesIv = response.json()["data"]["aesIv"]
secretKey = response.json()["data"]["secretKey"]

now = datetime.now()
timestamp = int(now.timestamp() * 1000)

sign = execjs.compile(open("./main.js", "r", encoding='utf-8').read()).call("GetSign", timestamp, client, product,
                                                                            secretKey)

data = {
    'i': query,
    'from': 'auto',
    'to': '',
    'useTerm': 'false',
    'dictResult': 'true',
    'keyid': 'webfanyi',
    'client': client,
    'product': product,
    'sign': sign,
    'appVersion': '1.0.0',
    'vendor': 'web',
    'pointParam': 'client,mysticTime,product',
    'mysticTime': timestamp,
    'keyfrom': 'fanyi.web',
    'mid': '1',
    'screen': '1',
    'model': '1',
    'network': 'wifi',
    'abtest': '0',
    'yduuid': 'abcdefg',
}

print(data)

response = requests.post('https://dict.youdao.com/webtranslate', headers=headers, data=data)
print(response.text)

res = execjs.compile(open("./main.js", "r", encoding='utf-8').read()).call("DecodeResult", response.text, aesKey, aesIv)
print(res)
